package com.wu.apiorderadd.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wu.apiorderadd.service.OrederService;
import com.wu.beans.Oreders;
import com.wu.vo.Reultvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrederServiceImpl implements OrederService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
//    @HystrixCommand(fallbackMethod = "fallbackSaveOrder",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
//    })
    public Reultvo save(Oreders oreders) {
        //1. 调⽤ order-add服务进⾏保存
        //order-add=localhost:9001
        //如果此时order-add对应多个服务，会进行负载均衡的轮询策略
        //参数1：访问服务的url
        //参数2：传递的对象参数
        //参数3：指定服务提供者返回的数据类型

        Reultvo vo = restTemplate.postForObject("http://order-add/order/add", oreders,   Reultvo.class);
        //get⽅式请求
//        restTemplate.getForObject();
//        String userId=oreders.getUserId();
//        Reultvo vo = restTemplate.getForObject("http://order-add/order/add?userId="+userId, Reultvo.class);
//        System.out.println(vo+"================"+vo);
        //2. 调⽤ orderitem-add 保存订单快照

        //3. 调⽤ stock-update 修改商品库存

        //4. 调⽤ shopcart-del 删除购物⻋记录
        return vo;
    }


    /**
     * 降级处理
     * @param order
     * @return
     */
    public Reultvo fallbackSaveOrder(Oreders order){
        return Reultvo.fail(400,"⽹络异常，请重试！");
    }
}
